package com.hecore.lagou.mr.partition;

import com.hecore.lagou.mr.entity.PartitionBean;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @Author: hecore
 * @Date: 2020/10/18 21:12
 * @Description:
 *  * 1. 读取⼀⾏⽂本，按照制表符切分
 *  * 2. 解析出appkey字段，其余数据封装为PartitionBean对象（实现序列化Writable接⼝）
 *  * 3. 设计map()输出的kv,key-->appkey(依靠该字段完成分区)，PartitionBean对象作为Value输出
 */

public class PartitionMapper extends Mapper<LongWritable, Text,Text, PartitionBean> {

    final PartitionBean bean=new PartitionBean();
    final Text k=new Text();

    // 1.1 重写map
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
       String[] fields=value.toString().split("\t");
       String appKey=fields[2];
        bean.setId(fields[0]);
        bean.setDeviceId(fields[1]);
        bean.setAppKey(appKey);
        bean.setIp(fields[3]);
        bean.setSelfDuration(Long.parseLong(fields[4]));
        bean.setThirdPartDuration(Long.parseLong(fields[5]));
        bean.setStatus(fields[6]);
        k.set(appKey);
        context.write(k,bean);
    }


}
